const Koa = require("./app/mykoa1");
const app = new Koa();
// 记录请求开始时间
let logger = async (ctx, next) => {
  const start = Date.now();
  console.log(`start: ${start}`);
  await next();
  const end = Date.now();
  console.log(`请求:  ${ctx.url} 耗时: ${end - start}ms`);
};
let sleep = (time) => {
  return new Promise((resolve) => setTimeout(resolve, time));
};
app.use(logger);
app.use(async (ctx, next) => {
  ctx.body = "1";
  await sleep(1000);
  await next();
  ctx.body += "2";
});
app.use(async (ctx, next) => {
  ctx.body += "3";
  await next();
  ctx.body += "4";
});
app.use((ctx, next) => {
  ctx.body += "5";
});

app.listen(8888, () => {
  console.log(`server is running on http://localhost:${8888}`);
});
